﻿using System;

namespace _5E.Utils.MathHelper.Extension
{
    /// <summary>
    /// 程序说明：算法扩展类
    /// 创建作者：ahl
    /// 创建时间：2012-03-25
    /// </summary>
    public static class MathExtension
    {
        #region 百分比

        /// <summary>
        /// 减少百分比，四舍五入
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="nowPercent">当前比例</param>
        /// <param name="maxPercent">最大比例</param>
        /// <returns></returns>
        public static int ReducePercent(this int value, int nowRate, int maxRate)
        {
            int[] values = GetPercentResult(value, nowRate, maxRate);
            return values[0] - values[1];
        }

        /// <summary>
        /// 增加百分比,四舍五入
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="nowRate">当前比例</param>
        /// <param name="maxRate">最大比例</param>
        /// <returns></returns>
        public static int IncreasePercent(this int value, int nowRate, int maxRate)
        {
            int[] values = GetPercentResult(value, nowRate, maxRate);
            return values[0] + values[1];
        }

        /// <summary>
        /// 获取百分比计算值
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="nowRate">当前比例</param>
        /// <param name="maxRate">最大比例</param>
        /// <returns></returns>
        static int[] GetPercentResult(int value, int nowRate, int maxRate)
        {
            int[] result = new int[2];
            result[0] = value;
            int temp = result[0] * nowRate;
            result[1] = temp / maxRate;
            result[1] += Convert.ToDouble(temp % maxRate) / maxRate >= 0.5 ? 1 : 0;
            return result;
        }

        #endregion

        #region 概率

        /// <summary>
        /// 触发概率
        /// </summary>
        /// <param name="now">当前概率</param>
        /// <param name="max">最大概率</param>
        /// <returns></returns>
        public static bool TriggleProbability(this int now, int max)
        {
            Probability prob = new Probability(now, max);
            return prob.IsTrigger;
        }

        /// <summary>
        /// 触发概率
        /// </summary>
        /// <param name="ran">随机对象</param>
        /// <param name="now">当前概率</param>
        /// <param name="max">最大概率</param>
        /// <returns></returns>
        public static bool TriggleProbability(this Random ran, int now, int max)
        {
            Probability prob = new Probability(ran, now, max);
            return prob.IsTrigger;
        }

        #endregion

        #region 随机抽数

        public static Int32 RandOne(this Int32 minNum, Int32 maxNum)
        {
            var result = 0;
            if (minNum < maxNum)
            {
                Random rd = new Random(Guid.NewGuid().GetHashCode());
                result = rd.Next(minNum, maxNum);
            }
            return result;
        }

        #endregion
    }
}
